package com.pig4cloud.pig.module.controller.intermediary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.intermediary.IntermediaryImg;
import com.pig4cloud.pig.module.service.intermediary.IIntermediaryImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 物业接待的图片Controller
 *
 * @author ruoyi
 * @date 2022-08-19
 */
@Validated
@Api(value = "物业接待的图片控制器", tags = {"物业接待的图片管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/img")
public class IntermediaryImgController {

    private final IIntermediaryImgService intermediaryImgService;

    /**
     * 查询物业接待的图片列表
     */
    @ApiOperation("查询物业接待的图片列表")
    @PreAuthorize("@pms.hasPermission('system:img:list')")
    @GetMapping("/list")
    public String list(String id) {
        QueryWrapper<IntermediaryImg> qw = new QueryWrapper<>();
        QueryWrapper<IntermediaryImg> mediary_id = qw.eq("mediary_id",id);
        TableDataInfo<IntermediaryImg> td = PageUtils.buildDataInfo(intermediaryImgService.page(PageUtils.buildPage(),qw));
        List<IntermediaryImg> list = td.getData();
        String Img = null;
        for (IntermediaryImg img:list){
            Img=img.getImgUrl();
        }
        return Img;
    }

    /**
     * 获取物业接待的图片详细信息
     */
    @ApiOperation("获取物业接待的图片详细信息")
    @PreAuthorize("@pms.hasPermission('system:img:query')")
    @GetMapping("/{id}")
    public R<IntermediaryImg> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        IntermediaryImg intermediaryImg = intermediaryImgService.getById(id);
        return R.ok(intermediaryImg);
    }

    /**
     * 新增物业接待的图片
     */
    @ApiOperation("新增物业接待的图片")
    @PreAuthorize("@pms.hasPermission('system:img:add')")
    @SysLog("物业接待的图片")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody IntermediaryImg intermediaryImg) {
        return R.okByRow(intermediaryImgService.save(intermediaryImg) ? 1 : 0);
    }

    /**
     * 修改物业接待的图片
     */
    @ApiOperation("修改物业接待的图片")
    @PreAuthorize("@pms.hasPermission('system:img:edit')")
    @SysLog("物业接待的图片")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody IntermediaryImg intermediaryImg) {
        return R.okByRow(intermediaryImgService.updateById(intermediaryImg) ? 1 : 0);
    }

    /**
     * 删除物业接待的图片
     */
    @ApiOperation("删除物业接待的图片")
    @PreAuthorize("@pms.hasPermission('system:img:remove')")
    @SysLog("物业接待的图片")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(intermediaryImgService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
